﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;

namespace SalesSys.BLL
{

    public class EmployeeBLL : BaseBLL<Employee, Guid>
    {

        protected IRepository<Department> _DepartmentRep;
        protected virtual IRepository<Department> DepartmentRep
        {
            get { return _DepartmentRep ?? (_DepartmentRep = RepositoryFactory.GetRepositoryByFactoryUnit<IRepository<Department>>()); }
            set { _DepartmentRep = value; }
        }

        public KeyValuePair<int, IEnumerable<Employee>> Select(string name, bool? isLeave,Guid? departmentId, int page=1,int rows=10)
        {
            var query = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(name))
            {
                query = query.Where(p => p.Name.Contains(name));
            }
            if (isLeave != null)
            {
                query = query.Where(p => p.IsLeave == isLeave);
            }
            if (departmentId!=null)
            {
                query = query.Where(p => p.Department.Id == departmentId);
            }
            var count = query.Count();
            var list = query.OrderByDescending(p=>p.CreateTime).Paging(page, rows).ToList();
            return new KeyValuePair<int, IEnumerable<Employee>>(count, list);
        }

        /// <summary>
        /// 通过名称判断：邮箱，手机，登录名，获取用户信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public  Employee GetUserByJudge(string name)
        {
            if (name == null)
                return null;
            Employee user = null;
            if (char.IsDigit(name, 0)) //手机
            {
                user = Rep.Entities.FirstOrDefault(p => p.Phone == name && !p.IsLeave);
            } //loginName
            else
            {
                user = Rep.Entities.FirstOrDefault(p => p.LoginName == name && !p.IsLeave);
            }
            return user;
        }

        /// <summary>
        /// 检测用户密码,密码验证办法：密码+盐码再md5
        /// </summary>
        /// <param name="name">帐号、邮箱、手机</param>
        /// <param name="md5PwS">密码+盐码再md5</param>
        /// <param name="salt">盐码</param>
        /// <returns>用户信息，null表示没有验证通过</returns>
        public Employee CheckPw(string name, string md5PwS, string salt)
        {
            var user = GetUserByJudge(name);
            if (user != null && !string.IsNullOrWhiteSpace(user.LoginPwd) && EncryptEx.MD5EncryptToBase64(user.LoginPwd + salt) == md5PwS)
            {
                return user;
            }
            return null;
        }
        /// <summary>
        /// 获取部门人员
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        public Employee[] GetDepartmentEmps(Guid depId)
        {
            return Rep.Entities.Where(p => p.Department.Id == depId).ToArray();
        }

        public IEnumerable<Department> GetDepartments(bool isRoot=false)
        {
            if (isRoot)
            {
               return  DepartmentRep.Entities.Where(p => p.Parent == null).ToArray();
            }
            return DepartmentRep.Entities.ToList();
        }
        public Department GetDepartment(Guid id)
        {
            return DepartmentRep.Find(id);
        }
        /// <summary>
        /// 获取上级部门管理人编号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Guid GetParentManagerId(Guid id)
        {
            return DepartmentRep.Find(id).Parent.ManagerId;
        }
        public Department[] GetDepartmentListByParentGuid(Guid parentGuid)
        {
            return DepartmentRep.Entities.Where(p => p.ParentId == parentGuid).ToArray();
        }

        /// <summary>
        /// 检测用户密码,密码验证办法：密码
        /// </summary>
        /// <param name="name">帐号、邮箱、手机</param>
        /// <param name="pwd">密码</param>
        /// <returns>用户信息，null表示没有验证通过</returns>
        public Employee CheckPw(string name, string pwd)
        {
            var user = GetUserByJudge(name);
            if (user != null && !string.IsNullOrWhiteSpace(user.LoginPwd) && user.LoginPwd == pwd)
            {
                return user;
            }
            return null;
        }

        /// <summary>
        /// 根据token获取用户
        /// </summary>
        /// <param name="token">Token，唯一标识</param>
        /// <returns>用户信息，null表示没有验证通过</returns>
        public Employee GetUserByToken(string token)
        {
            var user = Rep.Entities.FirstOrDefault(p => p.Token == token && !p.IsLeave);
            return user;
        }
    }
}
